import sys,os,string,errno

def mkdir_p(path):      #This function makes directory at the path specified. If directory is already there, then it handles the error.
    try:
        os.mkdir(path)  #making the directory by making system calls......
    except OSError as err:
        if err.errno==errno.EEXIST:
            pass        #If directory is there, then go ahead..... Don't stop by displaying message.......
        else:
            print "Error!!!\nUnable to create %s directory!!!" % path   #If unable to create directory when it doesn't exist, then display the error message....

def listdir_check(path):      #This function checks whether the path specified is correct and then lists the directories in the path......
    try:
        list=os.listdir(path) #This system call lists all the directories in the path specified.....
        return list
    except OSError as err:    #If error occurs, then perform the following tasks.....
        if err.errno==2:      #error no. 2 corresponds to the one when path not found.....
            print "\nDirectory path specified is wrong!! \nPlease check it...."
            exit(1)           #exit from the program with exit code specified as "1"
            

if __name__ == "__main__":    #Main function......
    print  "Started the task......"
    if len(sys.argv)<2:       #Checking that the correct number of arguments are specified.....
        print "Wrong number of arguments specified.... please check them....."
        print "\nThe syntax is \"Python <filename.py> <Directory where code is present>\" "
    else:
        path=sys.argv[1]      #path is specified in argument 1, so reading its value into a variable.....
        dirs=listdir_check(path)  #Checking the validity of the path and getting the list of directories present there if path specified is correct....
        dir_present=0         #This variable is to check whether the path specified contains the source directory inside it as well...
        #print dirs
        for dir in dirs:      #Taking all the directories and checking for the source directory inside the path specified.....
            if string.upper(dir)=="SOURCE":
                path_binaries=path+"/binaries"  #If source directory is present, then create a binaries directory where the libraries will be stored after compilation....
                mkdir_p(path_binaries)
                code_path=path+"/"+dir
                code_dirs=listdir_check(code_path) #Getting the list of all the directories present inside the source directory....
                #print code_dirs
                for code_dir in code_dirs:         #Now taking each directory present inside source directory.....
                    curr_dir=code_path+"/"+code_dir
                    os.chdir(curr_dir)             #This leads to setting up of the focus onto the code directory and we can now run configure commands in there....
                    mkdir_p(path_binaries+"/"+code_dir) #Creating a directory inside the binaries directory with the name as same of the name of the code directory....
                    #s.system("make dist clean")
                    os.system("./configure --prefix "+path_binaries+"/"+code_dir) #Running the configure command with the path of output directory specified....
                    os.system("make")              #Running the "make" command....
                    os.system("make install")      #Running the "make install" command....
                print "\n Job done......"
                dir_present=1                      #setting up the variable to indicate that source directory was present in the path specified....
                break                              #Exiting after processing the source directory....
        if dir_present==0:                         #Checking whether the directory "source" was present or not......
            print "\nNo source directory found in the path(%s) provided......" % path
            print "Please check the path value.... \nIt should contain source directory inside it....."
        